package bsts.view.subscription;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableColumn;
import javax.swing.table.TableModel;

import bsts.application.BSTSApplication;
import bsts.model.Account;
import bsts.model.AccountStatus;
import bsts.model.Customer;

public class ViewCustomerAccountsPanel extends JPanel {

	private static final long serialVersionUID = 1L;
	private JFrame containerParent;
	private JFrame container;
	private BSTSApplication manager;

	private JPanel pnlMain;
	private JPanel pnlCustomers;
	private JPanel pnlButtons;
	private JPanel pnlButtons_sub;

	private JTable tblAccounts;
	private JScrollPane scrAccounts;

	private JButton btnSelect;
	private JButton btnBack;

	private ActionListener actlis_btnSelect;
	private ActionListener actlis_btnBack;

	private ArrayList<Account> accounts;
	private Customer customer;

	public ViewCustomerAccountsPanel(JFrame containerParent, JFrame container,
			BSTSApplication manager, Customer cust) {
		this.containerParent = containerParent;
		this.container = container;
		this.manager = manager;
		this.customer = cust;
		this.accounts = customer.getAccounts();

		pnlMain = new JPanel();
		pnlCustomers = new JPanel();
		pnlButtons = new JPanel();
		pnlButtons_sub = new JPanel();

		pnlMain.setLayout(new BorderLayout());
		pnlCustomers.setLayout(new BorderLayout());
		pnlButtons_sub.setLayout(new GridLayout(3, 1, 0, 5));

		tblAccounts = new JTable();
		scrAccounts = new JScrollPane();

		initTbl();
		scrAccounts
				.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
		scrAccounts.add(tblAccounts, null);
		scrAccounts.setViewportView(tblAccounts);
		scrAccounts.setPreferredSize(new Dimension(300, 150));

		btnSelect = new JButton("Select");
		btnBack = new JButton("Back");

		pnlCustomers.add(scrAccounts, BorderLayout.CENTER);

		pnlButtons_sub.add(btnSelect);
		pnlButtons_sub.add(btnBack);

		pnlButtons.add(pnlButtons_sub);

		pnlMain.add(pnlCustomers, BorderLayout.WEST);
		pnlMain.add(pnlButtons, BorderLayout.EAST);

		this.add(pnlMain);

		initActionListener();
	}

	public void refreshTbl() {
		tblAccounts.revalidate();
		tblAccounts.repaint();
	}

	private void initTbl() {
		final String[] strHeaded = { "Account Number", "Status" };

		// Create a model of the data.
		TableModel dataModel = new AbstractTableModel() {
			private static final long serialVersionUID = 1L;

			public int getColumnCount() {
				return strHeaded.length;
			}

			public int getRowCount() {
				return accounts.size();
			}

			public Object getValueAt(int row, int col) {
				Account account = accounts.get(row);

				if (col == 0) {
					return account.getAccountNo();
				} else if (col == 1) {
					return account.getAccountStatus().toString();
				} else {
					return null;
				}
			}

			public String getColumnName(int column) {
				return strHeaded[column];
			}

			public Class<?> getColumnClass(int col) {
				return getValueAt(0, col).getClass();
			}
		};

		tblAccounts = new JTable(dataModel);
		tblAccounts.getSelectionModel();
		tblAccounts.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

		DefaultTableCellRenderer right = new DefaultTableCellRenderer();
		right.setHorizontalAlignment(DefaultTableCellRenderer.TRAILING);

		tblAccounts.setDefaultRenderer(Object.class, right);

		int[] intWidth = { 200, 100 };

		for (int i = 0; i < intWidth.length; i++) {
			TableColumn tblCol = tblAccounts.getColumn(strHeaded[i]);
			tblCol.setMaxWidth(intWidth[i]);
		}
	}

	private void initActionListener() {
		actlis_btnSelect = new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (tblAccounts.getSelectedRow() >= 0) {
					Account selectedAccount = accounts.get(tblAccounts
							.getSelectedRow());
					if (manager.getViewManager().getSubscriptionCreate()) {
						if (selectedAccount.getAccountStatus().equals(
								AccountStatus.Activated)) {
							manager.getViewManager().showNewSubscriptionWindow(
									container, selectedAccount, customer);
						} else {
							JOptionPane.showMessageDialog(container,
									"Please select an activated account.");
						}
					} else {
						manager.getViewManager().showUpdSubscriptionWindow(container,
								selectedAccount, customer);
					}

				} else
					JOptionPane.showMessageDialog(container,
							"Please select an account.");
			}
		};

		actlis_btnBack = new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				manager.getViewManager().goBackParent(containerParent,
						container);
			}
		};

		btnSelect.addActionListener(actlis_btnSelect);
		btnBack.addActionListener(actlis_btnBack);
	}
}
